package com.woniu.account.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.account.dto.Coach.CoachDto;
import com.woniu.account.form.User.*;
import com.woniu.account.form.coach.CoachLoginForm;
import com.woniu.account.form.coach.CoachRegisterForm;
import com.woniu.account.form.coach.PageCoachForm;
import com.woniu.account.form.coach.UpdateCoachForm;
import com.woniu.account.model.Coach;
import com.woniu.account.param.User.*;
import com.woniu.account.param.coach.CoachLoginParam;
import com.woniu.account.param.coach.CoachRegisterParam;
import com.woniu.account.param.coach.PageCoachParam;
import com.woniu.account.param.coach.UpdateCoachParam;
import com.woniu.account.service.CoachService;
import com.woniu.exception.userexp.YogaException;
import com.woniu.model.Result;
import com.woniu.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月11日
 */

@Api(tags = "教练操作信息接口")
@RestController
@RequestMapping("/coach")
public class CoachController {
    @Resource
    private CoachService coachService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private OSSUtil ossUtil;


    /**
     * 发送注册验证码的接口
     *
     * @param mobile
     * @return
     * @throws Exception
     */
    @GetMapping("/sendRegisterCode")
    @ApiOperation(value = "发送注册验证码的接口")
    public Result sendRegisterCode(@RequestParam("mobile") String mobile) throws Exception {
        coachService.sendRegisterCode(mobile);
        return Result.getResult().setMsg("发送注册验证码成功").setCode(200);
    }


    @GetMapping("/byId/{id}")
    @ApiOperation("根据id查找教练信息")
    public Result<CoachDto> byId(@PathVariable("id") Integer id) {
        Coach coach = coachService.getById(id);
        return Result.getResult(CoachDto.class).setData(BeanUtil.toBean(coach, CoachDto.class));
    }

    @PostMapping("/codeRegister")
    @ApiOperation(value = "教练点击注册接口")
    public Result registerByCode(@RequestBody CoachRegisterForm form) throws Exception {
        coachService.CodeRegister(BeanUtil.toBean(form, CoachRegisterParam.class));
        return Result.getResult().setCode(200).setMsg("成功注册");
    }



    @PostMapping("/login")
    @ApiOperation(value = "教练账号密码登录接口")
    public Result login(@RequestBody CoachLoginForm form) throws Exception {
        CoachDto dto = coachService.login(BeanUtil.toBean(form, CoachLoginParam.class));
        Map<String, Object> map = new HashMap<>();
        return Result.getResult().setData(dto).setMsg("登录成功");
    }

    @PostMapping("/updateImage")
    @ApiOperation("教练改个人头像")
    public Result updateImage(UpdateUserImageForm form) throws Exception {
        String token = WebUtil.getToken();
        String refreshToken = redisUtil.get(token);
        CoachDto coachDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getCoach(refreshToken)),CoachDto.class);
        if (!FileUtil.isImageFile(form.getFile().getInputStream())) {
            throw new YogaException(9999, "文件类型错误");
        }
        String oldName = form.getFile().getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String filePath = OSSUtil.uploadFile(form.getFile().getInputStream(), name + suffix, "woniupengyuyan");
        UpdateUserImageParam param = BeanUtil.toBean(form, UpdateUserImageParam.class);
        Coach byId = coachService.getById(coachDto.getCoachId());
        byId.setCoachImage(filePath);
        coachService.updateById(byId);
        return Result.getResult().setMsg("修改个人头像成功");
    }

    @Transactional
    @PostMapping("/Update")
    @ApiOperation("修改信息")
    public Result update(@RequestBody UpdateCoachForm form) throws Exception {
        UpdateCoachParam param = BeanUtil.toBean(form, UpdateCoachParam.class);
        CoachDto coachDto = coachService.updateCoach(param);
        return Result.getResult().setData(coachDto).setMsg("修改成功");
    }

    /**
     * 修改密码
     * @param mobile
     * @return
     * @throws Exception
     */









    /**
     * 分页查询所有教练
     *
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/pageAllCoach")
    @ApiOperation("分页查询所有用户")
    public Result pageAllUser(@RequestBody PageCoachForm form) throws YogaException {
        PageInfo<CoachDto> page = coachService.pageAllCoach(BeanUtil.toBean(form, PageCoachParam.class));
        return Result.getResult().setMsg("查询成功").setData(page);
    }

    @GetMapping("/loadCoachByIds")
    @ApiOperation("加载教练自己的信息")
    public Result loadCoachByIds() throws Exception{
        String token = WebUtil.getToken();
        String s = redisUtil.get(token);
        CoachDto coachDto1 = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getCoach(s)),CoachDto.class);
        Coach coach = coachService.getById(coachDto1.getCoachId());
        CoachDto coachDto = BeanUtil.toBean(coach, CoachDto.class);
        return Result.getResult().setData(coachDto);
    }

    @GetMapping("/changeCoachState/{coachId}")
    Result changeCoachState(@PathVariable("coachId") Integer coachId){
        Coach byId = coachService.getById(coachId);
        byId.setCoachState(2);
        coachService.updateById(byId);
        return Result.getResult().setMsg("成功");
    }

}


